
import 'package:dio/dio.dart';
import 'package:xreader/common/app_config.dart';
import 'package:xreader/common/secure_text.dart';
import 'package:xreader/dto/app_config_vo_entity.dart';
import 'package:xreader/dto/book_local_page_vo_entity.dart';
import 'package:xreader/generated/json/base/json_convert_content.dart';
import 'package:xreader/model/t_book_cont_entity.dart';
import 'package:xreader/model/t_book_entity.dart';
import 'package:xreader/vo/page_info.dart';

Dio dio = Dio(BaseOptions(
  headers: {},
));
class HttpCommmon {
  static  Pattern pathPrefix = RegExp("[/]");

  //GET 请求
  static get(String api,Map<String,dynamic>? params) async {
    print("api===>$api");
    Response response = await dio.get(api,queryParameters: params);
    return response;
  }

  //获取远程数据
  static Future<BookLocalPageVoEntity> getLocalAppBookPage(String api) async {
    Response resp = await get(api, null);
    Map<String,dynamic> apiRet = SecureText.aesDecrypt(resp.toString());
    BookLocalPageVoEntity result = BookLocalPageVoEntity.fromJson(apiRet);
    return result;
  }

  //获取远程书籍分页数据
  static Future<PageInfoVo<TBookEntity>> queryBookPageRemote(int pageIndex) async{
    PageInfoVo<TBookEntity> result = PageInfoVo();
    String api = AppConfig.bookPageListApi(pageIndex);
    Response resp = await get(api, null);
    Map<String,dynamic> json = SecureText.aesDecrypt(resp.toString());
    final int? count = jsonConvert.convert<int>(json['count']);
    final int? totalPage = jsonConvert.convert<int>(json['totalPage']);
    if (count != null) {
      result.count = count;
    }
    if(totalPage != null){
      result.totalPage = totalPage;
    }
    final List<TBookEntity>? books = jsonConvert.convertListNotNull<TBookEntity>(json['data']);
    if (books != null) {
      result.data = books;
    }
    return result;
  }

  //获取全局配置信息
  static Future<AppConfigVoEntity> getAppConfig(String api) async {
    Response resp = await get(api, null);
    Map<String,dynamic> apiRet = SecureText.aesDecrypt(resp.toString());
    AppConfigVoEntity result = AppConfigVoEntity.fromJson(apiRet);
    return result;
  }

  //获取书籍基本信息+章节信息
  static Future<TBookEntity> getBookInfo(String api) async {
    Response resp = await get(api, null);
    Map<String,dynamic> apiRet = SecureText.aesDecrypt(resp.toString());
    TBookEntity result = TBookEntity.fromJson(apiRet);
    return result;
  }

  //获取章节内容
  static Future<TBookContEntity> getBookContInfo(String api) async {
    Response resp = await get(api, null);
    Map<String,dynamic> apiRet = SecureText.aesDecrypt(resp.toString());
    TBookContEntity result = TBookContEntity.fromJson(apiRet);
    return result;
  }

}